package com.yamaha.bikedesalon.postItem;

import org.json.JSONObject;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.yamaha.bikedesalon.R;

public class BodyPostPartItem implements Item {
	// ============================================================
	private static class ItemHolder {
		ImageView icon;
		TextView title;
		TextView postTime;
		TextView brand;
		TextView price;
		TextView category;
	}

	// ============================================================
	private JSONObject postInfo = null;
	private final ImageLoader imageLoader;

	public BodyPostPartItem(ImageLoader imageLoader, JSONObject object) {
		this.imageLoader = imageLoader;
		postInfo = object;
	}

	@Override
	public JSONObject getObject() {
		// TODO Auto-generated method stub
		return postInfo;
	}

	@Override
	public int getViewType() {
		return ViewType.ITEM.ordinal();
	}

	@Override
	public View getView(LayoutInflater inflater, View convertView, int position) {
		ItemHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.post_part_item, null);
			convertView.setId(0x2033 + position);

			holder = new ItemHolder();
			holder.title = (TextView) convertView.findViewById(R.id.post_part_title);
			holder.brand = (TextView) convertView.findViewById(R.id.post_part_brand);
			holder.category = (TextView) convertView.findViewById(R.id.post_part_category);
			holder.price = (TextView) convertView.findViewById(R.id.post_part_price);
			holder.postTime = (TextView) convertView.findViewById(R.id.post_part_date_time);
			holder.icon = (ImageView) convertView.findViewById(R.id.post_part_image);

			convertView.setTag(holder);
		} else {
			holder = (ItemHolder) convertView.getTag();

		}

		try {

			final String icon = postInfo.getString("icon");
			if (icon == null || icon.length() == 0) {
				holder.icon.setImageBitmap(null);
			} else {
				imageLoader.displayImage(icon, holder.icon);
			}

			holder.title.setText(postInfo.getString("title"));
			holder.brand.setText("Brand : " + postInfo.getString("brand_name"));
			// will get category text before show to output
			holder.category.setText("Category : " + postInfo.getString("category_name"));
			holder.price.setText("Price : " + postInfo.getString("price") + " Bath");
			holder.postTime.setText(postInfo.getString("time"));

		} catch (Exception e) {
			Log.e("BodyPostPartItem View Error", e.getMessage(), e);
		}

		return convertView;
	}
}
